package Dispatcher;

import java.io.*;
import java.net.*;
import java.util.ArrayList;

/**
 * Group Dispatcher receives the floor calls and elevator destinations and spawns
 * various threads to send informations to different elevators
 * @author Varun Maryada, Sanat Dixit, Steve Tannahill
 *
 */
public class GroupDispatcher {
	static final int MAX_ELEVATORS = 256;
	static final int INIT = 1;
	static final int START = 2;
	static final int REQUESTUPDATE = 3;
	static final int UPDATE = 4;
	static final int HALLCALL = 5;
	private ServerSocket serverSocket;
	Socket tmpSocket;
	ArrayList<PrintWriter> elevators;
	BufferedReader in;

	public void handle(String msg) {
		byte[] bMsg = msg.getBytes();
		switch (bMsg[0]) {
		case INIT:
			System.out.printf("Initialize from Elevator:" + (int) bMsg[1]
					+ "\n");
			// create PrintWriter for this elevator
			try {
				elevators.add(bMsg[1] - 1, new PrintWriter(tmpSocket.getOutputStream(), true));
			} catch (IOException e) {}

			// send a start message to elevator
			elevators.get(bMsg[1] - 1).println("Start" + msg);
			break;
		case UPDATE:
			System.out.printf("Received update from Elevator" + (int) bMsg[1] + "\n");
			break;
		}
		System.out.println("Handling");
	}

	public GroupDispatcher() {
		elevators = new ArrayList<PrintWriter>();
		serverSocket = null;
		try {
			serverSocket = new ServerSocket(5000);

		} catch (IOException e) {
			e.printStackTrace(System.err);
			System.exit(1);
		}
	}
	public void activateSocket(){
		try {
			tmpSocket = serverSocket.accept();
			in = new BufferedReader(new InputStreamReader(tmpSocket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public void receivedHallCall(int floorNumber, int direction) {
		SendFloorRequestThread srt = new SendFloorRequestThread(floorNumber, direction, this);
		srt.start();
	}
	
	public void receivedElevatorCall(int destination) {
		SendElevatorRequestThread srt = new SendElevatorRequestThread(destination, this);
		srt.start();
	}
	
	public String receiveMessage() {
		String msg;
		try {
			msg = in.readLine();
			return msg;
		} catch (SocketException e2) {
			System.out.println("Done");
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace(System.err);
			System.exit(1);
		}
		return "Failed";
	}

	public void sendMessage(int elevatorNum, String Msg) {

	}

	@Override
	public void finalize() {
		try {
			// close all sockets and PrintWriters
			serverSocket.close();
			in.close();
		} catch (IOException e) {
			System.out.println("IO Exception");
		}
	}

	public static void main(String args[]) {
		String msg;
		System.out.println("Started dispatcher");
		GroupDispatcher gd = new GroupDispatcher();
		// create and start an update thread if needed
		UpdateThread ut = new UpdateThread(gd);
		ut.start();
		gd.activateSocket();
		while (true) {
			msg = gd.receiveMessage();
			HandleReceivedMsgThread hrm = new HandleReceivedMsgThread(gd, msg);
			hrm.start();
		}
	}
}





